package 树;

import java.util.Arrays;

public class Leedcode1046最后一块石头的重量 {

    public static void main(String[] args) {
        Leedcode1046最后一块石头的重量 o = new Leedcode1046最后一块石头的重量();
        System.out.println(o.lastStoneWeight(new int[]{1,1}));
    }

    public int lastStoneWeight(int[] stones) {
        int last = stones.length - 1;
        int pre = last - 1;
        int index = stones.length - 1;
        while (index > 0){
            Arrays.sort(stones,0,index+1);
            int max1 = stones[index];
            int max2 = stones[index - 1];
            if (max1 > max2){
                stones[index - 1] = max1 - max2;
                index = index - 1;
            }else {
                stones[index - 1] = max1 - max2;
                index = index - 2;
            }
        }
        return stones[0];
    }
}
class NodeTree {
    int val;
    NodeTree left;
    NodeTree right;

    public NodeTree(int val) {
        this.val = val;
    }

    public NodeTree() {
    }

    public NodeTree(int val, NodeTree left, NodeTree right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
